package com.example.transaction_springboot.controller;

import com.example.transaction_springboot.controller.request.AdminPageRequest;
import com.example.transaction_springboot.common.Result;
import com.example.transaction_springboot.entity.Admin;
import com.example.transaction_springboot.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/admin")
@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;


    @GetMapping("/list")
    public Result list() {
        return Result.success(adminService.list());
    }

    @GetMapping("/selectByPage")
    public Result selectByPage(AdminPageRequest adminPageRequest) {
        return Result.success(adminService.page(adminPageRequest));
    }

    @PostMapping("/addAdmin")
    public Result addUser(@RequestBody Admin admin) {
        adminService.addAdmin(admin);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        Admin admin = adminService.selectById(id);
        return Result.success(admin);
    }

    @PostMapping("/update")
    public Result updateById(@RequestBody Admin admin) {
        adminService.updateById(admin);
        return Result.success();
    }

    @GetMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        adminService.deleteById(id);
        return Result.success();
    }

    @PostMapping("/changePassword")
    public Result changePassword(@RequestBody Admin admin) {
        adminService.changePassword(admin);
        return Result.success();
    }

}
